package com.leetcode.problems_07;

import com.leetcode.problems_05.ListNode;

import static com.sun.tools.javac.jvm.ByteCodes.swap;

/**
 * 0024: 两两交换链表中的节点
 *      给定一个链表，两两交换其中相邻的节点，并返回交换后的链表。
 *      你不能只是单纯的改变节点内部的值，而是需要实际的进行节点交换。
 *
 *
 * @Author: WuPeng
 * @Date: 2021/5/18 3:02 下午
 */
public class ChangeLinkedNode {

    public static ListNode solution(ListNode head){

        ListNode tmp_head = new ListNode(-1);

        tmp_head.next = head;

        ListNode curr = tmp_head;

        while(curr.next != null && curr.next.next != null){
            ListNode tmp1 = curr.next;
            ListNode tmp2 = curr.next.next.next;

            curr.next = curr.next.next;
            curr.next.next = tmp1;
            curr.next.next.next = tmp2;

            curr = curr.next.next;
        }
        return tmp_head.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(0);
        ListNode one = new ListNode(1);
        ListNode two = new ListNode(2);
        ListNode three = new ListNode(3);
        ListNode four = new ListNode(4);
        ListNode five = new ListNode(5);
        head.next = one;
        one.next = two;
        two.next = three;
        three.next = four;
        four.next = five;

        ListNode newHead = solution(head);

        while (newHead != null){
            System.out.printf(newHead.val + ",");
            newHead = newHead.next;
        }

    }

}
